package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.warning.domain.entity.Device;
import com.ruoyi.warning.domain.entity.Property;
import com.ruoyi.warning.domain.entity.PropertyVminfoLog;
import com.ruoyi.warning.domain.query.PropertyQuery;
import com.ruoyi.warning.domain.vo.PropertyVminfoLogVO;
import com.ruoyi.warning.service.IDevicesService;
import com.ruoyi.warning.service.IPropertyService;
import com.ruoyi.warning.service.IPropertyVminfoLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 虚拟机资产日志表 前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-08-28
 */
@RestController
@RequestMapping("/propertyVminfoLog")
@Component("propertyVminfoLog")
@Api(tags = "虚拟机资产日志", description = "用于处理虚拟机资产日志相关操作的接口")
public class PropertyVminfoLogController {
    @Resource
    private IPropertyVminfoLogService propertyVminfoLogService;
    @Resource
    private IPropertyService propertyService;
    @Resource
    private IDevicesService devicesService;

    @ApiOperation("根据资产id和时间获取日志")
    @GetMapping(value = "/getLog")
    public R getLog(PropertyQuery query)
    {
        return R.ok(
                propertyVminfoLogService.list(
                        new LambdaQueryWrapper<PropertyVminfoLog>()
                                .eq(PropertyVminfoLog::getPropertyId, query.getPropertyId())
                                .ge(PropertyVminfoLog::getInserted, query.getStartTime())
                                .le(PropertyVminfoLog::getInserted, query.getEndTime())
                                .orderByAsc(PropertyVminfoLog::getInserted)));
    }

    @ApiOperation("根据资产id获取详情")
    @GetMapping(value = "/getDetail")
    public R getDetail(PropertyQuery query)
    {
        PropertyVminfoLog propertyVminfoLog = propertyVminfoLogService.getOne(
                new LambdaQueryWrapper<PropertyVminfoLog>()
                        .eq(PropertyVminfoLog::getPropertyId, query.getPropertyId())
                        .orderByDesc(PropertyVminfoLog::getInserted)
                        .last("limit 1"));
        Property property = propertyService.getById(query.getPropertyId());
        Device device = devicesService.getOne(
                new LambdaQueryWrapper<Device>()
                        .eq(Device::getHostname, property.getHostname())
                        .last("limit 1"));
        PropertyVminfoLogVO propertyVminfoLogVO= new PropertyVminfoLogVO();
        propertyVminfoLogVO.setPropertyVminfoLog(propertyVminfoLog);
        propertyVminfoLogVO.setProperty(property);
        propertyVminfoLogVO.setDevice(device);
        return R.ok(propertyVminfoLogVO);
    }

    @ApiOperation("刷新")
    @GetMapping(value = "/refresh")
    public void refresh(){
        propertyVminfoLogService.refresh();
    }

    @ApiOperation("根据hostname获取进程列表")
    @GetMapping(value = "/getOidProcessList/{hostname}")
    public R getOidProcessList(@PathVariable String hostname) {
        return R.ok(propertyVminfoLogService.getOidProcessList(hostname));
    }

    @ApiOperation("根据hostname获取磁盘列表")
    @GetMapping(value = "/getOidDiskList/{hostname}")
    public R getOidDiskList(@PathVariable String hostname) {
        return R.ok(propertyVminfoLogService.getOidDiskList(hostname));
    }
}
